package com.whq.thrift.api.web.admin.controller.bs.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.config.ErrorCode;
import com.whq.thrift.api.web.admin.service.config.ErrorCodeService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class ErrorCodeController {
	private static final int DEFAULT_PAGE_SIZE = 25;
	@Autowired
	private ErrorCodeService errorCodeService;

	@RequestMapping(value = "bs/config/errorcode/list")
	public String list(
			@RequestParam(value = "errorCode", required = false) String errorCode,
			@RequestParam(value = "businessType", required = false) String businessType, 
			@RequestParam(value = "moduleName", required = false) String moduleName,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,Model model) {
		PageData<ErrorCode> pageData = new PageData<ErrorCode>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		if(StringUtil.isNotBlank(errorCode)){
			whereParams.put("errorCode", errorCode);
		}
		
		if(StringUtil.isNotBlank(businessType)){
			whereParams.put("businessType", businessType);
		}
		
		if(StringUtil.isNotBlank(moduleName)){
			whereParams.put("moduleName", moduleName);
		}
		
		int count = this.errorCodeService.findRecordCount(whereParams);
		pageData.setTotalCount(count);

		int beginIndex = (currentPage - 1) * pageSize;
		List<ErrorCode> resultList = this.errorCodeService.findRecordsByMap(whereParams, beginIndex, pageSize);
		pageData.setResultList(resultList);
		
		model.addAttribute("errorCode", errorCode);
		model.addAttribute("businessType", businessType);
		model.addAttribute("moduleName", moduleName);
		model.addAttribute("pageData", pageData);
		
		return "bs/config/errorcode/list";
	}
	
	@RequestMapping(value = "bs/config/errorcode/show")
	public String show(@RequestParam(value = "id", required = true) String id, Model model) {
		if (id == null) {
			return "error";
		}
		ErrorCode record = this.errorCodeService.findRecordByKey(id);
		model.addAttribute("record", record);
		if (record == null) {
			return "error";
		}
		return "bs/config/errorcode/show";
	}
	
	@RequestMapping(value="bs/config/errorcode/showAdd")
	public String showAdd(Model model){
		ErrorCode record = new ErrorCode();
		model.addAttribute("record", record);
		return "bs/config/errorcode/showAdd";
	}
	
	@RequestMapping(value="bs/config/errorcode/add")
	public String add(@Valid ErrorCode record, 
			BindingResult result, 
			Model model) {
		ErrorCode existRecord = this.errorCodeService.findRecordByKey(record.getErrorCode()+"");
		if (existRecord != null) {
			model.addAttribute("errMsg", "error_code已存在，不能重复！！");
			model.addAttribute("record", record);
			return "bs/config/errorcode/showAdd";
		}
		
		this.errorCodeService.addRecord(record);
		return "redirect:show.do?id=" + record.getErrorCode();
	}
	
	@RequestMapping(value="bs/config/errorcode/showUpd")
	public String showUpd(@RequestParam(value="id", required=true) String id, 
			Model model){
		if (id == null) {
			return "error";
		}
		ErrorCode record = this.errorCodeService.findRecordByKey(id);
		if (record == null) {
			return "error";
		}
		model.addAttribute("record", record);
		return "bs/config/errorcode/showUpd";
	}
	
	@RequestMapping(value="bs/config/errorcode/upd")
	public String upd(@Valid ErrorCode record, 
			BindingResult result, Model model) {
		if (result.hasErrors()) {
			model.addAttribute("record", record);
			return "bs/config/errorcode/showUpd";
		}
		
		this.errorCodeService.updateRecord(record);
		return "redirect:show.do?id=" + record.getErrorCode();
	}
	
	@RequestMapping(value="bs/config/errorcode/del")
    public String del(@RequestParam(value = "id", required = true, defaultValue = "0") String id,
            Model model) {
        this.errorCodeService.deleteRecord(id);
        return "bs/config/errorcode/delOk";
    }
	
	@RequestMapping(value="bs/config/errorcode/delRecords")
    public String delRecords(String[] delIds,
            Model model) {
        if (delIds != null) {
            for (int i = 0; i < delIds.length; i++) {
                if (delIds[i] != null) {
                    this.errorCodeService.deleteRecord(delIds[i]);
                }
            }
        }
        return "redirect:list.do";
    }
}
